<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:t="http://www.w3.org/1999/xhtml">
<head th:replace="components/easyui/easyui-list :: head('权限', 'easyui')">
</head>
<body>
<div id="tb">
    <div class="datagrid-toolbar" style="padding-bottom: 6px">
        用户id: <input class="easyui-textbox" style="width: 160px" id="search_userId" name="userId" data-options="required:false">
        用户名: <input class="easyui-textbox" style="width: 160px" id="search_username" name="username" data-options="required:false">
        权限: <t:dict class="easyui-combobox" id="search_authority" name="authority" dict-name="role_type"  style="width:160px" allow-empty="true"></t:dict>
        <a href="javascript:void(0)" class="easyui-linkbutton c-primary" style="width:80px" onclick="doSearch()"><i class="fa fa-search" aria-hidden="true"></i> <span th:text="#{search}"></span></a>
        <a href="javascript:void(0)" class="easyui-linkbutton c-basic" style="width:80px" onclick="doSearchReset()"><i class="fa fa-repeat" aria-hidden="true"></i> <span th:text="#{reset}"></span></a>
    </div>
    <div class="datagrid-toolbar easyui-panel" style="padding:5px;">
        <a href="javascript:void(0)" class="easyui-linkbutton c-primary" style="width:80px" data-options="toggle:true,group:'g1'" onclick="doAdd()"><i class="fa fa-plus" aria-hidden="true"></i> <span th:text="#{add}"></span></a>
        <a href="javascript:void(0)" class="easyui-linkbutton c-warning" style="width:80px" data-options="toggle:true,group:'g1'" onclick="doEdit()"><i class="fa fa-pencil" aria-hidden="true"></i> <span th:text="#{edit}"></span></a>
        <a href="javascript:void(0)" class="easyui-linkbutton c-danger" style="width:80px" data-options="toggle:true,group:'g1'" onclick="doDelete()"><i class="fa fa-trash" aria-hidden="true"></i> <span th:text="#{delete}"></span></a>
    </div>
</div>
<table id="tt" data-options="url:'/api/authorities/easyui/list',method:'get',animate: true,rownumbers:true,fit:true,toolbar: '#tb', pagination: true,idField:'userId', singleSelect: true, selectOnCheck: true, checkOnSelect: true">
    <thead>
    <tr>
        <th data-options="field:'ck',checkbox:true"></th>
        <th data-options="field:'userId',width:200,sortable:true,align:'center'">用户id</th>
        <th data-options="field:'username',width:200,sortable:true,align:'center'">用户名(昵称)</th>
        <th data-options="field:'authority',width:200,sortable:true,align:'center'">权限</th>
    </tr>
    </thead>
</table>
<div id="addModal" class="easyui-window" title="录入" data-options="modal:true,closed:true,collapsible:false" style="width:640px;height:480px;padding:10px;">
    <form id="form_add">
        <div>
            <t:select id="add_userId" allow-empty="false" name="userId" order="desc" query="t_users,username,id" class="easyui-combobox" style="width:100%" data-options="label:'用户名:'"></t:select>
        </div>
        <div>
            <t:dict class="easyui-combobox" id="add_authority" name="authority" dict-name="role_type" style="width:100%" data-options="label:'权限:', multiple:true"></t:dict>
        </div>
        <div style="text-align:center;padding:5px 0">
            <a href="javascript:void(0)" class="easyui-linkbutton c-primary" style="width:80px" onclick="add()" th:text="#{ok}"></a>
            <a href="javascript:void(0)" class="easyui-linkbutton c-basic" style="width:80px" onclick="clearForm()" th:text="#{cancel}"></a>
        </div>
    </form>
</div>

<div id="editModal" class="easyui-window" title="编辑" data-options="modal:true,closed:true,collapsible:false" style="width:640px;height:480px;padding:10px;">
    <form id="form_edit">
        <div>
            <t:select id="edit_userId" name="userId" order="desc" query="t_users,username,id" class="easyui-combobox" style="width:100%" data-options="label:'用户名:'"></t:select>
        </div>
        <div>
            <t:dict class="easyui-combobox" id="edit_authority" name="authority" dict-name="role_type" style="width:100%" data-options="label:'权限:', multiple:true"></t:dict>
        </div>
        <div style="text-align:center;">
            <a href="javascript:void(0)" class="easyui-linkbutton c-primary" style="width:80px" onclick="edit()" th:text="#{ok}"></a>
            <a href="javascript:void(0)" class="easyui-linkbutton c-basic" style="width:80px" onclick="clearForm()" th:text="#{cancel}"></a>
        </div>
    </form>
</div>
<div th:replace="components/easyui/easyui-list :: js('easyui')"></div>
<script>
    function doSearch() {
        $('#tt').datagrid('load', {
            userId: $('#search_userId').val(),
            username: $('#search_username').val(),
            authority: $('#search_authority').val()
        });
    }

    function doSearchReset() {
        $('#search_userId').textbox('setValue', '');
        $('#search_username').textbox('setValue', '');
        $('#search_authority').combobox('select', '');
        doSearch();
    }

    function doAdd() {
        $('#addModal').window('open');
    }

    function doEdit() {
        //获取选中的第一行数据
        //var row = $('#tt').datagrid('getSelected');
        //获取选中的所有行数据
        let rows = $('#tt').datagrid('getSelections');
        if(rows.length == 1){
            let row = rows[0];
            console.log(row);
            $('#edit_userId').combobox('setValue', row.userId);
            $('#edit_authority').combobox('setValues', row.authority);
            $('#edit_userId').combobox('readonly',true);
            $('#editModal').window('open');
        }else{
            $.messager.alert('提示信息','请选择一条数据！','error');
        }
    }

    function doDelete() {
        deleteRequestByUserId('/api/authorities/userIds/');
    }

    function add() {
        doRequest({
            formId: '#form_add',
            url: '/api/authorities',
            type: 'POST'
        });
    }

    function edit() {
        doRequest({
            formId: '#form_edit',
            url: '/api/authorities',
            type: 'PATCH'
        });
    }

    function clearForm() {
        $('#form_add').form('clear');
        $('#addModal').window('close');

        $('#form_edit').form('clear');
        $('#editModal').window('close');
    }

    function formatIcon(val, row){
        return '<i class="' + val + '" aria-hidden="true"></i>';
    }
</script>
</body>
</html>